//
//  BiUnderLineControl.m
//  GottaWash
//
//  Created by bindu on 14-8-19.
//  Copyright (c) 2014年 QiHe. All rights reserved.
//

#import "BiUnderLineControl.h"

@implementation BiUnderLineControl
@synthesize delegate;
@synthesize drawText;
@synthesize fontSize;
@synthesize textColor;
@synthesize textHightlightColor;
@synthesize hightlight;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.fontSize = 14;
        self.textColor = UIColorFromRGB(0x67b8fa);
        self.textHightlightColor = [UIColor grayColor];
        self.hightlight = NO;
    }
    return self;
}

- (void)dealloc
{
    self.textColor = nil;
    
    [super dealloc];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    UIFont * drawFont = [UIFont systemFontOfSize:self.fontSize];
    
    CGSize szText = [BiUtil calcTextSize:self.drawText textFont:drawFont width:200];
    
    if (self.hightlight)
    {
        CGContextSetFillColorWithColor(context, self.textHightlightColor.CGColor);
    }
    else
    {
        CGContextSetFillColorWithColor(context, self.textColor.CGColor);
    }
    CGRect rtText = CGRectMake(0, (self.frame.size.height-szText.height)/2, self.frame.size.width, szText.height);
    [self.drawText drawInRect:rtText withFont:drawFont lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];
    
    CGPoint startPoint = CGPointMake((self.frame.size.width-szText.width)/2, (self.frame.size.height-szText.height)/2 + szText.height + 2);
    CGPoint endPoint = CGPointMake((self.frame.size.width-szText.width)/2+szText.width, (self.frame.size.height-szText.height)/2 + szText.height + 2);
    
    if (self.hightlight)
    {
        [self drawLineFrom:startPoint to:endPoint color:self.textHightlightColor];
    }
    else
    {
        [self drawLineFrom:startPoint to:endPoint color:self.textColor];
    }
}

//直线
-(void)drawLineFrom:(CGPoint)startPoint
                 to:(CGPoint)endPoint
              color:(UIColor*)color
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);  //  线的宽度
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    CGContextStrokePath(context);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.hightlight = YES;
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.hightlight = NO;
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    self.hightlight = NO;
    [self setNeedsDisplay];
    
    if (nil != self.delegate)
    {
        [self.delegate onUnderlineControlTouched:self];
    }
}

@end
